package com.confee.guanglai.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.confee.guanglai.commons.GlobleConstants;
import com.confee.guanglai.domain.User;
import com.confee.guanglai.repository.UserRepository;

/**
 * 员工Service.
 * 
 * @author 李忠
 * 
 */
@Service
@Transactional
public class UserService {

	@Autowired
	private UserRepository userRepository;

	public User save(User user) {
		return userRepository.save(user);
	}

	public User update(User user) {
		return userRepository.save(user);
	}

	public Integer delete(Long id) {
		return userRepository.modifyById(id, GlobleConstants.STATUS_INVALID);
	}

	public User get(Long id) {
		return userRepository.findOne(id);
	}

	public Page<User> find(String value, Pageable pageable) {
		if (StringUtils.isNotBlank(value)) {
			return userRepository.findByValue(value, "%" + value + "%", pageable);
		} else {
			return userRepository.findAllUser(pageable);
		}

	}

	public User findByNo(String no) {
		return userRepository.findByNoAndStatus(no, GlobleConstants.STATUS_NORMAL);
	}

}
